
package com.scjyzr.mysweeper;

import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;

import java.io.IOException;

/**
 * 震动类
 * 
 * @author Administrator
 */
public class MyVibrator {
    private static MyVibrator myVibrator = null;

    private Vibrator mVibrator = null;

    private MyVibrator() {
    }

    private long[] mWeakPattern = new long[] {
            0, 10, 0, 0
    };

    public static MyVibrator getVibrator() {
        if (myVibrator == null) {
            myVibrator = new MyVibrator();
        }
        return myVibrator;
    }

    /**
     * @param context
     * @param milliseconds 震动时间长度
     */
    public void IsVibrator(Context context, long milliseconds) {
        if (mVibrator == null) {
            mVibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
        }
        mVibrator.cancel();
        mVibrator.vibrate(milliseconds);
    }

    /**
     * @param context
     */
    public void IsVibrator(Context context) {
        if (mVibrator == null) {
            mVibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
        }
        mVibrator.cancel();
        mVibrator.vibrate(mWeakPattern, -1);
    }

    /**
     * 播放声音
     */
    public void IsMediaPlayer() {
        try {
            MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource("/system/media/audio/ui/VideoRecord.ogg");
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
            mediaPlayer.prepare();
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.release();
            }
            mediaPlayer.start();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
